<?php

/**
 * 获取用户的设备相关信息
 */
namespace app\shell;


use app\service\UserDeviceBindingService;
use app\service\UserDeviceShareService;
use share\console\CConsole;
use share\console\CLIRunner;
use share\service\UserBaseService;
use share\utils\Helper;

class CUserDevice extends CLIRunner{

    public function get_device_action($userId = ''){
        if(empty($userId)){
            $userId = CConsole::prompt("input the userId",'');
        }
        $userInfo = UserBaseService::getBase($userId);
        if(empty($userInfo)){
            CConsole::print_line("user info of ".$userId." is not exist");
        }

        $res = [];

        // 获取用户分享的设备
        $shareUserData = UserDeviceShareService::getAllByShareUser($userInfo['user_id']);

        // 获取用户绑定的设备
        $bindingData = UserDeviceBindingService::getAllByUser($userInfo['user_id']);

        // 用户绑定设备分享出去的信息
        $shareDeviceData = [];

        
        if($bindingData){
            $iotIds = array_column($bindingData,'iot_id');
            $shareDeviceData = UserDeviceShareService::getAllByMainId($iotIds);
        }

        $shareUids = Helper::arraySmartUnique(array_column($shareDeviceData,'share_id'));
        $shareMainUids = Helper::arraySmartUnique(array_column($shareUserData,'user_id'));
        $allUserId = Helper::arraySmartUnique(array_merge($shareUids,$shareMainUids));
        $shareUserInfo = UserBaseService::getAllByIds($allUserId);

        foreach($shareUserData as $shareData){
            $mainUser = Helper::filterOne($shareUserInfo,['user_id'=>$shareData['user_id']]);
            $res[] = [
                "iotId" => $shareData["iot_id"],
                "owned" => 0,
                "userId" => $mainUser ? $mainUser['user_id'] : '',
                "userIdentity" => $mainUser ? $mainUser['identity_id'] : '',
                "rule" => $shareData['rule'],
            ];
        }

        foreach($bindingData as $bind){
            $tmp = [
                "iotId" => $bind['iot_id'],
                "owned" => 1,
                "userId" => $userInfo["user_id"],
                "userIdentity" => $userInfo["identity_id"],
                "shareInfo" => []
            ];
            if($shareDeviceData){
                $shareDevices =  Helper::filter($shareDeviceData,['main_id'=>$bind['iot_id']]);
                if($shareDevices){
                    foreach($shareDevices as $shareDevice){
                        $currentShareUser = Helper::filterOne($shareUserInfo,['user_id' => $shareDevice['share_id']]);
                        $tmp['shareInfo'][] = [
                            "shareId" => $currentShareUser ? $currentShareUser['user_id'] :'',
                            "nickName" => $currentShareUser ? $currentShareUser['nick_name']:'',
                            "picture" => $currentShareUser ? $currentShareUser['picture']:'',
                            "rule" => $shareDevice['rule'],
                            "shareIdentity" => $currentShareUser ? $currentShareUser['identity_id']:'',
                        ];
                    }
                }
            }
            $res[] = $tmp;
        }  
        return $res;
    }

}